package com.zhiyou100.review.day015.homework;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;

import java.util.*;

/**
 * @packageName: Homework
 * @className: com.zhiyou100.review.day015.homework
 * @Description: TODO june 23,2020 作业
 * @author: yang
 * @date: 6/23/20
 * <p>
 * 1   文档总结
 * 2   通过键盘输入一个字符串   使用arraylist和hashset 获取每个字符出现的次数
 * 3   手写代码：遍历collection、arraylist、hashset的所有方式
 * 4   创建一个student类 属性 age sex score name 创建10个student对象装入hashset中
 * 通过设置student类保证age相差5岁以内 sex相同 name相同 score相差10以内为同一个学生  不能重复加
 * 5   创建一个teacher类 属性age name salary  创建10个teacher对象 转入treeset中
 * 通过设置teacher类 保证age大的在treeset前面  age相同 salary小的在treeset前面
 * salary相同 name小的在treeset前面
 * 6  第五题扩展：三个属性值都相同的为同一个对象不能重复加
 */
public class Homework {

    /**
     * @Description: TODO arraylist 获取每个字符出现的次数
     * @name: getThenNumberOfArraylist
     * @param: []
     * @return: void
     * @date: 6/23/20 19:28
     * @author: yang
     */
    private void getThenNumberOfArraylist() {
        String s = new Scanner(System.in).nextLine();
        ArrayList<Object> list1 = new ArrayList<>();
        ArrayList<Object> list2 = new ArrayList<>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            list1.add(c);

            if (!(list2.contains(c))) {
                list2.add(c);
            }
        }


        for (Object o : list2) {
            int cnt = 0;
            Character o1 = (Character) o;
            for (Object o2 : list1) {
                Character o21 = (Character) o2;
                if (o1.equals(o2)) {
                    cnt++;
                }
            }
            System.out.println(o1 + " 出现了 " + cnt + " 次");

        }

    }

    /**
     * @Description: TODO HashSet 获取字符出现的次数
     * @name: getThenNumberOfHashSet
     * @param: []
     * @return: void
     * @date: 6/23/20 19:32
     * @author: yang
     */
    private void getThenNumberOfHashSet() {
        String s = new Scanner(System.in).nextLine();
        ArrayList<Character> list = new ArrayList<>();
        HashSet<Character> hashSet = new HashSet<>();

        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            list.add(c);
            hashSet.add(c);
        }

        for (Object o : hashSet) {
            int cnt = 0;
            Character o1 = (Character) o;
            for (Object o2 : list) {
                Character o21 = (Character) o2;
                if (o1.equals(o2)) {
                    cnt++;
                }
            }
            System.out.println(o1 + " 出现了 " + cnt + " 次");

        }

    }


    private void printCollection1(Collection<Object> collection) {
        for (Object o : collection) {
            System.out.println(o);
        }

    }

    private void printCollection2(Collection<Object> collection) {
        Object[] objects = collection.toArray();
        for (Object object : objects) {
            System.out.println(object);
        }


    }

    private void printCollection3(Collection<Object> collection) {
        Iterator<Object> iterator = collection.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }


    private void printArrayList1(ArrayList<Object> collection) {
        for (Object o : collection) {
            System.out.println(o);
        }
    }

    private void printArrayList2(ArrayList<Object> collection) {
        Object[] objects = collection.toArray();
        for (Object object : objects) {
            System.out.println(object);
        }


    }

    private void printArrayList3(ArrayList<Object> collection) {
        Iterator<Object> iterator = collection.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    private void printArrayList4(ArrayList<Object> collection) {
        for (int i = 0; i < collection.size(); i++) {
            Object o = collection.get(i);
            System.out.println(o);
        }
    }

    private void printHashSet1(HashSet<Object> hashSet) {
        for (Object o : hashSet) {
            System.out.println(o);
        }

    }

    private void printHashSet2(HashSet<Object> hashSet) {
        Object[] objects = hashSet.toArray();
        for (Object object : objects) {
            System.out.println(object);
        }


    }

    private void printHashSet3(HashSet<Object> hashSet) {
        Iterator<Object> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }


    public static void main(String[] args) {
        HashSet<Student> students = new HashSet<>();
        int max = 10;
        for (int i = 0; i < max; i++) {
            int age = RandomUtil.randomInt(10, 15);
            char gender = RandomUtil.randomBoolean() ? '男' : '女';
            double score = NumberUtil.round(RandomUtil.randomDouble(60, 70), 2).doubleValue();
            String name = "张" + RandomUtil.randomInt(1, 5);
            Student student = new Student(age, gender, score, name);
            System.out.println(student);
            students.add(student);
        }

        System.out.println("=============");

        for (Student student : students) {
            System.out.println(student);
        }

        System.out.println("=============");

        TreeSet<Teacher> teachers = new TreeSet<>(new TeacherComparator());
        for (int i = 0; i < max; i++) {
            int age = RandomUtil.randomInt(20, 30);
            String name = "张" + RandomUtil.randomInt(1, 100);
            double salary = NumberUtil.round(RandomUtil.randomDouble(3000, 5000), 2).doubleValue();
            Teacher teacher = new Teacher(age, name, salary);
            System.out.println(teacher);
            teachers.add(teacher);
        }
        System.out.println("=============");
        System.out.println();
        for (Teacher teacher : teachers) {
            System.out.println(teacher);
        }


    }
}

class Teacher {
    private int age;
    private String name;
    private double salary;

    public Teacher() {
    }

    /**
     * @Description: TODO  三个属性值都相同的为同一个对象不能重复加
     * @name: equals
     * @param: [obj]
     * @return: boolean
     * @date: 6/23/20 20:48
     * @author: yang
     */
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Teacher)) {
            throw new RuntimeException("必须 是 Teacher 和 Teacher 比较");
        }
        Teacher teacher = (Teacher) obj;
        return teacher.getName().equals(this.name) && teacher.getAge() == this.age && teacher.getSalary() == this.salary;
    }

    @Override
    public int hashCode() {
        return 0;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Teacher(int age, String name, double salary) {
        this.age = age;
        this.name = name;
        this.salary = salary;
    }
}

class Student {
    private int age;
    private char gender;
    private double score;
    private String name;

    public Student() {

    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student(int age, char gender, double score, String name) {
        this.age = age;
        this.gender = gender;
        this.score = score;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", gender=" + gender +
                ", score=" + score +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public int hashCode() {
        return 1;
    }

    /**
     * @Description: TODO  通过设置student类保证age相差5岁以内 sex相同 name相同 score相差10以内为同一个学生  不能重复加
     * @name: equals
     * @param: [obj]
     * @return: boolean
     * @date: 6/23/20 20:15
     * @author: yang
     */
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Student)) {
            throw new RuntimeException("只能和 Student 对象使用 equals");
        }

        Student student = (Student) obj;
        boolean age = Math.abs(this.age - student.age) <= 5;
        boolean gender = student.gender == this.gender;
        boolean name = student.name.equals(this.name);
        boolean score = Math.abs(this.score - student.score) <= 10;

        return age && gender && name && score;

    }
}

class TeacherComparator implements Comparator<Teacher> {

    /**
     * @Description: TODO   保证age大的在treeset前面  age相同 salary小的在treeset前面 salary相同 name小的在treeset前面
     * @name: compare
     * @param: [o1, o2]
     * @return: int
     * @date: 6/23/20 20:37
     * @author: yang
     */
    @Override
    public int compare(Teacher o1, Teacher o2) {
        if (o1.getAge() == o2.getAge()) {
            // age 相同
            if (o1.getSalary() == o2.getSalary()) {
                // salary 相同，name小的在前
                return (o1.getName().compareTo(o2.getName()));

            } else {
                // salary 不相同, salary 小的在前
                if (o1.getSalary() > o2.getSalary()) {
                    return 1;
                } else {
                    return -1;
                }
            }
        } else {
            // age 不相同， age 大的在前
            if (o1.getAge() > o2.getAge()) {
                return -1;
            } else {
                return 1;
            }
        }
    }
}
